/**
 * Displays the XML Code for the test set currently being edited / created. 
 */

FBL.ns(function()
{
    with ( FBL )
    {

        function TestFireCodePanel()
        {
        }
        TestFireCodePanel.prototype = extend(Firebug.Panel,
        {
            name: "TestFireCodePanel",
            title: "XML",
            searchable: false,
            editable: true,
            lastTestSet: null,
            asString: '',
            parentPanel: "TestFireTreePanel",

            initializeNode: function(node)
            {
                addStyleSheet(node.ownerDocument, createStyleSheet(node.ownerDocument, "chrome://TestFireExtension/content/TestFire.css"));
            },

            rebuild: function(testSet)
            {
                try
                {
                    if ( !testSet && !this.lastTestSet )
                    {
                        return;
                    }
                    if ( !testSet )
                    {
                        testSet = this.lastTestSet;
                    }
                    while ( this.panelNode.firstChild )
                    {
                        this.panelNode.removeChild(this.panelNode.firstChild);
                    }


                    this.asString = new String();
                    var lines = testSet.toXMLStringAry();
                    var depth = 0;
                    var tab = '\t';
                    var tabs = new Array();
                    tabs.toString = function(){
                        var ret = '';
                        for(var _L = 1; _L < this.length; _L++ ){
                            ret += this[_L];
                        }
                        return ret;
                    }
                    for(var i = 0,  n = lines.length; i <n; ++i){
                        var line = lines[i];
                        var isTerminus =line.indexOf("</") >=0;
                        if(!isTerminus && line.indexOf("<") == 0){
                            tabs.push(tab);
                        }
                        this.asString += tabs.toString() + line + '\n';

                        if(line.indexOf("/>") > 0){
                            tabs.pop();
                        }
                        else if(isTerminus){
                            tabs.pop();
                        }

                    }
                    var ta = document.createElement('textbox');
                    ta.setAttribute("multiline", true);
                    ta.setAttribute("rows", i+ Math.round(i*.25));
                    ta.setAttribute("cols", "100");
                    ta.setAttribute("value", this.asString);

                    var div = document.createElement('div');
                    div.appendChild(ta);
                    this.panelNode.appendChild(div);
                    this.lastTestSet = testSet;
                }
                catch( ex )
                {
                    Firebug.TestFire.util.tf_debug(ex);
                }
            },

            _refresh: function()
            {
                var testset = Firebug.TestFire.getTestset();
                if(testset){
                    this.rebuild(testset);    
                }
            },

            

            supportsObject: function( object )
            {
                return typeof(object) != "undefined";
            },

            copyTestFire: function(object){
              copyToClipboard(this.asString);
            },


            getContextMenuItems: function( node, target )
            {
                try
                {
                    var items = [];
                    items.push(
                        "-",
                    {nol10n:1, label: "Copy To Clipboard", command: bindFixed(this.copyTestFire, this, "SomeObjectText") },
                                        {nol10n:1, label: "Refresh", command: bindFixed(this._refresh, this) }
                        );
                }
                catch( ex )
                {
                    Firebug.TestFire.util.tf_debug(ex);
                }

                return items;
            }

        });

        Firebug.registerPanel(TestFireCodePanel);


    }
});